const express = require("express");
const app = express();

app.use(express.static("public"));
app.use(express.json());  // 解析json请求体

const users = [
  {
    id: 1,
    name: "jack",
  },
  {
    id: 2,
    name: "rose",
  },
];

app.get("/users", (req, res) => {
  res.json({
    code: 200,
    message: "",
    success: true,
    data: users,
  });
});

app.post("/user", (req, res) => {
  const { name } = req.body;
  users.push({
    id: users.length,
    name,
  });
  res.json({
    code: 200,
    message: "",
    success: true,
    data: null,
  });
});

app.listen(3000, "localhost", (err) => {
  if (err) console.log(err);
  else console.log("服务器启动成功了: http://localhost:3000");
});
